package com.bwie.commodity.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.bwie.commodity.domain.dto.CommodityCategoryDTO;
import com.bwie.commodity.domain.vo.CommodityCategoryVO;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bwie.common.log.annotation.Log;
import com.bwie.common.log.enums.BusinessType;
import com.bwie.common.security.annotation.RequiresPermissions;
import com.bwie.commodity.domain.CommodityCategory;
import com.bwie.commodity.service.ICommodityCategoryService;
import com.bwie.common.core.web.controller.BaseController;
import com.bwie.common.core.web.domain.AjaxResult;
import com.bwie.common.core.utils.poi.ExcelUtil;
import com.bwie.common.core.web.page.TableDataInfo;

/**
 * 商品分类Controller
 *
 * @author ruoyi
 * @date 2024-11-14
 */
@RestController
@RequestMapping("/category")
public class CommodityCategoryController extends BaseController
{
    @Autowired
    private ICommodityCategoryService commodityCategoryService;

    /**
     * 查询商品分类列表
     */
    @RequiresPermissions("commodity:category:list")
    @GetMapping("/list")
    public TableDataInfo list(CommodityCategory commodityCategory)
    {
        startPage();
        List<CommodityCategory> list = commodityCategoryService.selectCommodityCategoryList(commodityCategory);
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @RequiresPermissions("commodity:category:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommodityCategory commodityCategory)
    {
        List<CommodityCategory> list = commodityCategoryService.selectCommodityCategoryList(commodityCategory);
        ExcelUtil<CommodityCategory> util = new ExcelUtil<CommodityCategory>(CommodityCategory.class);
        util.exportExcel((jakarta.servlet.http.HttpServletResponse) response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @RequiresPermissions("commodity:category:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(commodityCategoryService.selectCommodityCategoryById(id));
    }

    /**
     * 新增商品分类
     */
    @RequiresPermissions("commodity:category:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid CommodityCategory commodityCategory)
    {
        return toAjax(commodityCategoryService.insertCommodityCategory(commodityCategory));
    }

    /**
     * 修改商品分类
     */
    @RequiresPermissions("commodity:category:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid CommodityCategory commodityCategory)
    {
        return toAjax(commodityCategoryService.updateCommodityCategory(commodityCategory));
    }

    /**
     * 删除商品分类
     */
    @RequiresPermissions("commodity:category:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(commodityCategoryService.deleteCommodityCategoryByIds(ids));
    }

    /**
     * 商品分类级联下拉框
     */
    @GetMapping("getCategoryTree")
    public AjaxResult getCategoryTree(){
        List<CommodityCategoryDTO> list=commodityCategoryService.getCategoryTree();
        return success(list);
    }
}
